﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using JinianNet.JNCms.Filters;
using JinianNet.JNCms.Models;

namespace JinianNet.JNCms.Controllers
{
    [ManagerFilter()]
    public class ContentController : ControllerBase
    {

        public ActionResult List(int item, int page = 1, int size = 0)
        {
            if (size <= 0)
                size = Config.Page.Size;
            var list = Data.DataAccess.CreateContent().GetList(item, page, size);

            string home, pre, next;
            if (page > 1)
            {
                home = Url.Action("List", "Content", new { item = item });
                pre = Url.Action("List", "Content", new { page = page - 1, item = item });

            }
            else
            {
                home = null;
                pre = null;
            }
            if (list.Count == size)
            {
                next = Url.Action("List", "Article", new { page = page + 1, item = item });
            }
            else
            {
                next = null;
            }
            ViewData["WebPage"] = JinianNet.Web.PageLinker.GetActionLinker(home, pre, next);
            return View(list);
        }

        //[Authorize(Users="")]
        public ActionResult Edit(string item)
        {
            Content model = Data.DataAccess.CreateContent().GetItem(item);
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        //[Authorize(Users = "")]
        public ActionResult Edit(Content model)
        {
            if (Data.DataAccess.CreateContent().Update(model) > 0)
            {
                return Content(Config.GetTips("操作成功", Url.Action("List", "Content", new { item = model.ClassID }), true));
            }
            else
            {
                return Content(Config.GetTips("编辑失败，请重试", Url.Action("Edit", "Content", new { model = model }), false));
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        //[Authorize(Users = "")]
        public ActionResult Insert(Content model)
        {
            if (string.IsNullOrEmpty(model.KeyName))
            {
                model.KeyName = string.Concat(JinianNet.Common.Random.RandAlphabet(1), JinianNet.Common.Random.RandNumber(9));
            }
            else
            {
                if (!JinianNet.Common.Utils.IsNumberOrLetter(model.KeyName.Replace("-", "").Replace("_", ""), true))
                {
                    return Content(Config.GetTips("对不起，模块标识只能为字母与数字的组合，且只能以字母开头", null, false));
                }
            }
            model.CreateTime = DateTime.Now;
            Data.DataAccess.CreateContent().Insert(model);
            return Content(Config.GetTips("恭喜您，操作成功！", Url.Action("List", "Content", new { item = model.ClassID }), true));
        }

        //[Authorize(Users = "")]
        public ActionResult Insert(int item)
        {
            Content model = new Content();
            model.ClassID = item;
            model.KeyName = string.Concat(JinianNet.Common.Random.RandAlphabet(1), JinianNet.Common.Random.RandNumber(9));
            model.CreateTime = DateTime.Now;
            return View("Edit", model);
        }

        public ActionResult Delete(string item)
        {
            Data.IContent dal = Data.DataAccess.CreateContent();
            if (item.IndexOf(',') < 0)
                dal.Delete(int.Parse(item));
            else
                foreach (string value in item.Split(','))
                    if (!string.IsNullOrEmpty(value))
                        dal.Delete(int.Parse(value));
            return Content(Config.GetTips("恭喜您，操作成功！", null, true));
        }
    }
}
